<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
		<link rel="stylesheet" href="../../../../layuiadmin/layui/css/layui.css" media="all">
		<link rel="stylesheet" href="../../../../layuiadmin/style/admin.css" media="all">
		<link rel="stylesheet" href="../../../../css/common.css" media="all">

	</head>

	<body>

		<div class="layui-fluid">
			<div class="layui-row" id="view">
				<form class="layui-form" lay-filter="test_detail">
					<div class="layui-card">
						<div class="layui-card-body">
							<div class="layui-form-item">
								<label class="layui-form-label">分类 <span style="color:red">*</span></label>
								<div class="layui-input-inline">
									<input type="text" name="cat_name" lay-verify="required" placeholder="请选择分类" autocomplete="off" class="layui-input cat_name">
									<input type="hidden" name="cat_id" lay-verify="required" autocomplete="off" class="layui-input cat_id">
								</div>
								<div class="layui-form-mid layui-word-aux">
									<a href="Javascript:;" onclick="searchdata()">选择</a>
								</div>
							</div>
							<!--<div class="layui-form-item">
								<label class="layui-form-label">选择频道*</label>
								<div class="layui-input-inline">
									<input type="text" name="channel_name" lay-verify="required"  placeholder="选择频道" autocomplete="off" class="layui-input channel_name"> 
									<input type="hidden" name="channel_id" lay-verify="required" autocomplete="off" class="layui-input channel_id">
								</div>
								<div class="layui-form-mid layui-word-aux">
									<a href="Javascript:;" onclick="searchdata1()">选择</a>
								</div>
							</div>-->
							<div class="layui-form-item">
								<label class="layui-form-label">状态</label>
								<div class="layui-input-block on_off">
									<input type="radio" name="status" value="1" title="启用" />
									<input type="radio" name="status" value="0" title="停用" />
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">标题</label>
								<div class="layui-input-block">
									<input type="text" name="title" placeholder="请输入标题" autocomplete="off" class="layui-input">
								</div>
							</div>
							<!--<div class="layui-form-item">
								<label class="layui-form-label">排序</label>
								<div class="layui-input-inline">
									<input type="text" name="oid" placeholder="请输入排序" autocomplete="off" class="layui-input" >
								</div>
							</div>-->
							<div class="layui-form-item">
								<label class="layui-form-label">文章简介</label>
								<div class="layui-input-block">
									<input type="text" name="intro" placeholder="请输入文章简介" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">关键词</label>
								<div class="layui-input-block">
									<input type="text" name="keywords" placeholder="请输入关键词" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">作者</label>
								<div class="layui-input-inline">
									<input type="text" name="author" placeholder="作者名" autocomplete="off" class="layui-input"> 
								</div>
							</div> 
							<div class="layui-form-item">
								<label class="layui-form-label">缩略图</label>
								<div class="layui-input-block">
									<button type="button" class="layui-btn layui-btn-normal" onclick="choose(1)">上传图片</button>
									<ul class="show_img_ul" id="thumbnail"></ul>
								</div>
							</div>		
							<div class="layui-form-item">
								<label class="layui-form-label">来源链接</label>
								<div class="layui-input-block">
									<input type="text" name="resource_url" placeholder="来源链接" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item"> 
								<div class="layui-input-block">
									<div class="layui-upload">
									  <button type="button" class="layui-btn" id="test3"><i class="layui-icon"></i>上传文件</button>
									</div>  
								</div>
							</div> 
							<div class="layui-form-item">
								<label class="layui-form-label">内容</label>
								<div class="layui-input-block">
									<textarea class="layui-textarea layui-hide" name="content" lay-verify="content" id="demo_editor"></textarea>
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">备注</label>
								<div class="layui-input-block">
									<input type="text" name="notes" placeholder="请输入备注" autocomplete="off" class="layui-input">
								</div>
							</div>
							<!--<div class="layui-form-item">
								<label class="layui-form-label">来源</label>
								<div class="layui-input-block">
									<input type="text" name="resource" placeholder="请输入来源" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">来源地址</label>
								<div class="layui-input-block">
									<input type="text" name="resource_url" placeholder="请输入来源地址" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">文章链接</label>
								<div class="layui-input-block">
									<input type="text" name="url" placeholder="请输入文章链接" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">文章短连接</label>
								<div class="layui-input-block">
									<input type="text" name="url_short" placeholder="请输入文章短连接" autocomplete="off" class="layui-input">
								</div>
							</div>-->
						</div>
					</div>
					<div class="layui-form-item layui-layout-admin">
						<div class="layui-input-block">
							<div class="layui-footer" style="left: 0;"> 
								<button class="layui-btn" lay-submit lay-filter="demo1">立即提交</button>
							</div>
						</div>
					</div>
				</form>
			</div>
		</div>
		<script src="../../../../layuiadmin/layui/layui.js"></script>
		<script src="../../../../js/jquery.min.js"></script>
		<script src="../../../../js/common.js"></script>
		<script>
			var _data = {
				id: getParam('id')
			}
			layui.config({
				base: '../../../../layuiadmin/' //静态资源所在路径
			}).extend({
				tinymce: '../tinymce/tinymce',
				index: 'lib/index' //主入口模块
			}).use(['index', 'form', 'laytpl', 'laydate', 'upload', 'uploadnew','layedit','tinymce','util'], function(){
				var $ = layui.$,
					admin = layui.admin,
					laytpl = layui.laytpl,
					laydate = layui.laydate,
					upload = layui.upload,
					uploadnew = layui.uploadnew,
					layedit = layui.layedit,
					tinymce = layui.tinymce,
					util = layui.util,
					form = layui.form;

				var img_arr1 = [];
				var img_arr2 = [];
				
				
				if (_data.id) {
					initdata();
				}
				layedit.set({
					//暴露layupload参数设置接口 --详细查看layupload参数说明
					uploadFiles: {
						url: server.upimg,
						accept: 'file',
						acceptMime: 'file/*',
						size: '20480',
						autoInsert: true, //自动插入编辑器设置
						done: function(data) {
							console.log(data);
						}
					}, 
					//右键删除图片/视频时的回调参数，post到后台删除服务器文件等操作，
					//传递参数：
					//图片： imgpath --图片路径
					//视频： filepath --视频路径 imgpath --封面路径
					//附件： filepath --附件路径
					//测试参数
					backDelImg: true, //开发者模式 --默认为false
					devmode: true, //是否自动同步到textarea
					autoSync: true, //内容改变监听事件
					onchange: function(content) {
						// console.log(content);
					},  //插入代码设置 --hide:false 等同于不配置codeConfig
					codeConfig: {
						hide: true, //是否隐藏编码语言选择框
						default: 'javascript', //hide为true时的默认语言格式
						encode: true,
						class: 'layui-code' //默认样式
					},
					facePath: 'http://knifez.gitee.io/kz.layedit/Content/Layui-KnifeZ/',
					devmode: true,
					videoAttr: ' preload="none"',
					tool: [
						'html', 'undo', 'redo', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'removeformat',
						'fontFomatt', 'fontfamily', 'fontSize', 'fontBackColor', 'colorpicker', 'face', '|', 'left', 'center',
						'right', '|', 'link', 'unlink', 'images', 'image_alt', 'video', 'attachment', 'anchors', '|', 'table',
						'customlink', 'fullScreen', 'preview'
					],
					height: '500px'
				});
				var edit = tinymce.render({
					elem: "#demo_editor",
					height: 600
				});
				util.event('lay-event', {
					preveiw: function() {
						layer.open({
							type: 2,
							title: '预览',
							shadeClose: false,
							shade: 0.5,
							maxmin: true, //开启最大化最小化按钮
							area: ['60%', '80%'],
							content: 'pre_content.html',
							end: function() {},
							success: function(layero, index) {
								var body = layer.getChildFrame('body', index);
								body.find(".title").html($('input[name=title]').val());
								body.find(".showcontent").html(edit.getContent());
							}
						});
					},
				});
				//监听提交
				form.on('submit(demo1)', function(data) {
					var data = data.field;
					console.log(data)
					console.log(_data)
					_data = Object.assign(_data, data);
					_data.thumbnail = img_arr1.join(',');
					_data.resource_url = data.resource_url ? data.resource_url : img_arr2.join(',');
					_data.content = edit.getContent();
					layer.confirm('确定要提交数据？', function() {
						loading();
						if(_data.id) {
							submitdata(_data,server.sys_news_edit);
						} else {
							delete _data.id;
							submitdata(_data,server.sys_news_insert);
						}
					}, function() {
						layer.msg('取消操作');
					});
					return false;
				});
				window.searchdata1 = function(type) {
					layer.open({
						type: 2,
						title: '选择',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['500px', '98%'],
						content: 'choose_channel.html',
						end: function() {							
							console.log(_data.channel_id),
							console.log(_data.channel_name)
						},
						success: function() {
						}
					});
				}
				window.searchdata = function (type) {
					layer.open({
						type: 2,
						title: '选择',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['500px', '90%'],
						content: 'choose_cat.html',
						end: function() {},
						success: function() {}
					});
				}	
				function submitdata(_data,_url) {
					$.post(_url, _data, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								layer.alert('操作成功', function() {
									parent.layer.closeAll();
									parent.layui.table.reload('test-table-operate');
								});
								layer.closeAll('loading');
							} else {
								layer.msg(res.msg);
								layer.closeAll('loading');
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}

				window.choose = function(data) {
					layer.open({
						type: 2,
						title: '上传图片',
						shadeClose: false,
						shade: 0.5,
						maxmin: true, //开启最大化最小化按钮
						area: ['600px', '400px'],
						content: 'up_report.html?type=' + data,
						end: function() {},
						success: function() {}
					});
				}
				upload.render({
				    elem: '#test3'
				    ,url:  server.upimg //改成您自己的上传接口
				    ,accept: 'file' //普通文件
				    ,done: function(res){
				    	closeloading() 
				        $("input[name=resource_url]").val(res.file)
				        layer.msg('上传成功');
				        console.log(res);
				    }
				    ,before: function(obj){
					    loading()
					}
				    ,error:function(res){ 
				      closeloading() 
				    },
				  });
				window.getimg = function(data, type) {
					if(data) {
						switch(type) {
							case '1':
								createli(data, 'thumbnail', type)
								break;
							default:
								break;
						}
					}
				}
				window.delimg = function(i, type, _self) {
					var _type = String(type)
					layer.confirm('确定要删除？', function(index) {
						switch(_type) {
							case '1':
								img_arr1.splice(i, 1)
								break;
							default:
								break;
						}
						$(_self).parent().remove();
						layer.close(index)
					}, function() {
						layer.msg('取消操作');
					});
				}

				function createli(data, dom, type) {
					var lihtml = '';
					switch(type) {
						case '1':
							if(data.indexOf(',') == -1) {
								img_arr1.splice(0, 1, data)
							} else {
								for(var i = 0; i < data.split(',').length; i++) {
									img_arr1.push(data.split(',')[i])
								}
							}
							for(var i = 0; i < img_arr1.length; i++) {
								lihtml += '<li class="show_img_li">' +
									'<img src=' + img_arr1[i] + ' >' +
									'<p onclick="delimg(' + i + ',1,this)">删除</p>' +
									'</li>'
							}
							break;
						default:
							break;
					}
					$('#' + dom).html(lihtml);
				}

				function initdata() {
					$.post(server.sys_news_detail, _data, function(res, status) {
						if(status == 'success') {
							if(res.code == 0) {
								var data = res.news
								data.data.thumbnail ? createli(data.data.thumbnail, 'thumbnail', '1') : '',
								data.data.resource_url ? data.data.resource_url : '';
								edit.setContent(data.data.content)
								form.val("test_detail", data.data);
								form.render();
							} else {
								layer.msg(res.msg);
							}
						} else {
							layer.msg('服务器连接错误');
						}
					});
				}
				
				var htmlSelect = function(optionData, selectname) {
					var html = '';
					html += '<div class="layui-input-inline">';
					html += ' <select lay-filter=' + selectname + ' name=' + selectname + '>';
					html += '  <option value="">请选择</option>';
					for(var i = 0; i < optionData.length; i++) {
						html += ' <option value=' + optionData[i].id + ' title=' + optionData[i].id + '>' + optionData[i].name +
							'</option>';
					}
					html += ' </select>';
					html += '</div>';
					return html;
				}
			});
		</script>
	</body>

</html>